package collectiondemo;

import java.util.*;

public class TestDriver2 {

    public static void main(String[] args) {
        System.out.println("=====Collections工具类===ArrayList工具类排序==Collections.sort(list);========");
        test01();
        System.out.println("=====Collections工具类===Set set = new TreeSet();s.sort(list);========");
        test011();
        System.out.println("=====Collections工具类===//匿名内部类\n" +
                "        Collections.sort(list, new Comparator<Teacher>() ========");
        test012();
    }
    /**
     * Collections工具类的使用
     */
    public  static void test01(){
        //对list集合中的各个元素进行排列
        List<String> list = new ArrayList();
        //添加元素
        list.add("lisi ");
        list.add("dcsdcsdcd ");
        list.add("csdbcsdc  ");
        list.add("sdcdcdscd ");
        list.add("dubvdbvbvdb");
        list.add("cdbibowbdobdqb");
        System.out.println("排序之前：");
        printList(list);

        //使用工具类进行排序
        Collections.sort(list);//从大到小排序
        System.out.println("排序之后：");
        //排序之后遍历结果
        printList(list);
    }

    /**
     * Collections工具类的使用
     */
    public static  void test011(){
        //对list集合中的各个元素进行排序
        List<Student> list = new ArrayList();
        //添加元素
        list.add(new Student("zhangshu","10110",78));
        list.add(new Student("wnagwu ","10111",96));
        list.add(new Student("xiaoming ","102145",35));
        list.add(new Student("lihua","10245",95));

        Set set = new TreeSet();
        // ? 发生了运行时异常？ 使Student类实现 Comparatable接口
        set.add(new Student("傻逼还吃不吃","123456",75));
        System.out.println("排序之前：");
        printListStu(list);

        //使用工具类进行排序
        Collections.sort(list);//使用Student类实现，Comparatable接口

        System.out.println("排序之后");
        //排序之后遍历结果
        printListStu(list);

    }

    /**
     * collections工具类的使用
     */
    public static void test012(){
        //对list集合中的各个元素进行排序
        List<Teacher> list = new ArrayList();
        //添加元素
        list.add(new Teacher("zhangshu","10110",78));
        list.add(new Teacher("wnagwu ","10111",96));
        list.add(new Teacher("xiaoming ","102145",35));
        list.add(new Teacher("lihua","10245",95));

        Set set = new TreeSet();
       // set.add(new Student("傻逼还吃不吃","123456",75));
        System.out.println("排序之前：");
        printListTea(list);

        //使用工具类进行排序
        //匿名内部类
        Collections.sort(list, new Comparator<Teacher>() {
            @Override
            public int compare(Teacher tea1, Teacher tea2) {
                //业务逻辑处理的地方
                //默认值，默认等于
                int i = 0;
                if (tea1!=null&&tea2!=null){
                    if (tea1.getTeaScore()>tea2.getTeaScore()){
                        i=1;
                    }
                    if (tea1.getTeaScore()<tea2.getTeaScore()){
                        i=-1;
                    }
                }
                return i;
            }
        });//你们内部类提供Teacher类的比较器对象

        System.out.println("排序之后：");
        printListTea(list);

    }

    /**
     * 打印list中的元素对象
     * list被打印遍历的list
     */

    public  static void printList(List<String> list){
        //foreach循环遍历打印元素值
        for (String str:list){
            System.out.println("  "+str);
        }
        //换行
        System.out.println();
    }


    /**
     * 打印list中的元素对象
     * list 被打印遍历的list
     */
    private static  void printListStu(List<Student> list){
        for (Student str:list) {
            System.out.println("   "+str);
        }
        //换行
        System.out.println();
    }

    /**
     * 打印list中的元素对象
     * list 被打印遍历的list
     */
    private static  void printListTea(List<Teacher> list){
        for (Teacher tea:list) {
            System.out.println("   "+tea);
        }
        //换行
        System.out.println();
    }

}
